﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Services;
using DCS.ComLib.Config;
using VietThuongLMS.Utils;
using VietThuongLMS.Areas.Admin.Models;
using System.IO;

namespace VietThuongLMS.Areas.Admin.Controllers
{
    public class LoaiTinTucController : Controller
    {
        //
        // GET: /Admin/LoaiTinTuc/

        //[Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            if (!Request.IsAuthenticated)
                return RedirectToAction("Login", "Home", new { area = "Admin" });

            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            LoaiTinTucViewModel LoaiTinTucList = LoaiTinTucService.GetLoaiTinTucList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "LoaiTinTuc", "Index", "?p=", LoaiTinTucList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(LoaiTinTucList);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Detail(int id)
        {
            LoaiTinTuc LoaiTinTuc = LoaiTinTucService.GetLoaiTinTucById(id);
            return View(LoaiTinTuc);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(LoaiTinTucModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    LoaiTinTuc data = new LoaiTinTuc();
                    data.TenLoaiTinTuc = model.TenLoaiTinTuc;
                    bool result = LoaiTinTucService.InsertLoaiTinTuc(data);
                    if (result)
                        return RedirectToAction("Index", "LoaiTinTuc", new { area = "Admin" });
                    return View();
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

        //delete Account
        //[Authorize(Roles = "Admin")]
        [Authorize(Roles = "Admin")]
        [HttpGet]
        public ActionResult Delete(int id)
        {
            bool result = LoaiTinTucService.DeleteLoaiTinTuc(id);
            if (result)
            {
                return RedirectToAction("Index", "LoaiTinTuc", new { area = "Admin" });
            }
            else
            {
                return View();
            }

        }

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            LoaiTinTuc LoaiTinTuc = LoaiTinTucService.GetLoaiTinTucById(id);
            LoaiTinTucModel model = (new LoaiTinTucConverter()).LoaiTinTucToModel(LoaiTinTuc);
            return View(model);
        }

        [Authorize(Roles = "Admin")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(LoaiTinTucModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    LoaiTinTuc data = new LoaiTinTuc();
                    data.ID = model.ID;
                    data.TenLoaiTinTuc = model.TenLoaiTinTuc;
                    bool result = LoaiTinTucService.Edit(data);
                    if (result)
                        return RedirectToAction("Index", "LoaiTinTuc", new { area = "Admin" });
                    LoaiTinTuc LoaiTinTuc = LoaiTinTucService.GetLoaiTinTucById(model.ID);
                    LoaiTinTucModel model1 = (new LoaiTinTucConverter()).LoaiTinTucToModel(LoaiTinTuc);
                    return View(model1);
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

    }
}
